package leetcode.L152;

class Solution {
    public int maxProduct(int[] nums) {
        int[] minArr = new int[nums.length];
        int[] maxArr = new int[nums.length];

        for (int i = 0; i < nums.length; i++) {
            minArr[i] = nums[i];
            maxArr[i] = nums[i];
        }

        for (int i = 1; i < nums.length; i++) {
            maxArr[i] = Math.max(maxArr[i - 1] * nums[i], Math.max(nums[i], minArr[i - 1] * nums[i]));
            minArr[i] = Math.min(minArr[i - 1] * nums[i], Math.min(nums[i], maxArr[i - 1] * nums[i]));
        }

        int ans = maxArr[0];
        for (int i = 1; i < nums.length; ++i) {
            ans = Math.max(ans, (int) maxArr[i]);
        }
        return ans;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.maxProduct(new int[]{2, 3, -2, 4}));
    }
}